#include "udpserver.h"

UdpServer::UdpServer(QObject *parent, quint16 port) : QObject(parent)
{
    serverThread = new UdpServerThread(this, port);
    connect(serverThread, &UdpServerThread::datagramReceived, this, &UdpServer::handleDatagramReceived);
}

void UdpServer::start()
{
    if (serverThread != nullptr) {
        serverThread->start();
    }
}

void UdpServer::setIPandPort(QString ip, quint16 port)
{
    this->ip = ip;
    this->port = port;
}

void UdpServer::handleDatagramReceived(const QByteArray &datagram, const QHostAddress &sender, quint16 senderPort)
{
    qDebug() << "Received from" << sender.toString() << ":" << senderPort << " - " << datagram;
}

UdpServer::~UdpServer()
{
    serverThread->quit();
    serverThread->wait();
}
